package com.gxa.jjty.phy.service.impl;

import com.gxa.jjty.core.pojo.Evaluate;
import com.gxa.jjty.core.pojo.EvaluateImg;
import com.gxa.jjty.core.pojo.MyEvaluateVo;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import com.gxa.jjty.phy.mapper.EvaluateImgMapper;
import com.gxa.jjty.phy.mapper.EvaluateMapper;
import com.gxa.jjty.phy.mapper.MyEvaluateVoMapper;
import com.gxa.jjty.phy.service.EvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName EvaluateServiceImpl
 * @Author: ws
 * @date 2021/12/8 10:02
 */
@Service("evaluateService")
public class EvaluateServiceImpl extends BaseServiceImpl<Evaluate> implements EvaluateService {

    @Autowired
    EvaluateMapper evaluateMapper;

    @Autowired
    EvaluateImgMapper evaluateImgMapper;

    @Autowired
    MyEvaluateVoMapper myEvaluateVoMapper;

    @Override
    public Integer insertImg(Long orderId, String imgUrls) {

        String[] urls = null;
        EvaluateImg evaluateImg = null;
        int updates = 0;
        if (imgUrls.length() > 0) {
//            urls = imgUrls.substring(1, imgUrls.length() - 1).split(",");
            urls = imgUrls.split(",");
            for (int i = 0; i < urls.length; i++) {

                evaluateImg = new EvaluateImg(orderId,urls[i]);

                System.out.println(evaluateImg);
                updates += evaluateImgMapper.insertSelective(evaluateImg);
            }
        }

        return updates;
    }

    @Override
    public List<MyEvaluateVo> getMyEvaluate(Long phyId) {

        // 仅回去当前陪护员待评价的，拼接条件
        Example example = new Example(MyEvaluateVo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phyId",phyId);
        criteria.andEqualTo("flag",2);
        List<MyEvaluateVo> myEvaluateVos = myEvaluateVoMapper.selectByExample(example);

        // 当用陪护员有评论才查询详细信息
        if (myEvaluateVos.size()>0) {
            EvaluateImg evaluateImg =  new EvaluateImg();
            for (MyEvaluateVo myEvaluateVo : myEvaluateVos) {
                evaluateImg.setOrderId(myEvaluateVo.getOrderId());

                List<EvaluateImg> select = evaluateImgMapper.select(evaluateImg);
                myEvaluateVo.setImgUrls(select);
            }
        }

        return myEvaluateVos;
    }
}
